package cn.micai.base.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/20 14:45
 */
public class AppendToFileExample {

    private static final String fileName = "D://newFile.txt";

    public static void main(String [] args) {

        BufferedWriter bufferedWriter = null;
        FileWriter fileWriter = null;

        try {
            String data = " 你好";
            File file = new File(fileName);
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // true = append file
            fileWriter = new FileWriter(file.getAbsoluteFile(), true);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
